package hl.alde;

class  binarySearch {

    public static int binary(int[] a , int n , int x){
        int left = 0, right = n-1;
        while(left <= right){
            int middle = (left+right)/2;
            System.out.println("left = "+ left + " right = "+ right + " middle = "+ middle);
            if (x == a[middle])
                return middle;
            else if(x > a[middle])
                left = middle + 1;
            else
                right = middle - 1;
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] a={1,3,4,5,6,7,9,12,23,22};
        int k = binary(a, a.length, 4);
        System.out.println("k = " + k);
    }
}
